$(function(){
    /*Comportamento loader*/
    $('#loader').ajaxStart(function(){
        $(this).fadeIn('def', function(){});
    });
    $('#loader').ajaxStop(function(){
        $(this).fadeOut('def', function(){});
    });

    $('#filter #from').datepicker({
        dateFormat: 'dd/mm/yy'
    });
    $('#filter #to').datepicker({
        dateFormat: 'dd/mm/yy'
    });
    $('#ui-datepicker-div').attr('style', 'display:none')

    /*Eventi delle liste*/
    $('#node #list').change(function(){
        loadNodeInfo($(this).val()); //Carica le informazioni del sensore selezionato
        loadSensors('#sensor #list', $(this).val()); //Carica i sensori per il nodo selezionato
    });

    $('#sensor #list').change(function(){
        loadSensorInfo($(this).val(),$('#sensor #list option:selected').text()); //Carica le informazioni del sensore selezionato
    });

    $('#filter #search').click(function(){
        if($('#filter #list option:selected').val() != "per"){
            loadValues($('#node #list option:selected').val(),$('#sensor #list option:selected').text(),$('#filter #list option:selected').val());
        }
        else{
            loadValuesByDate($('#node #list option:selected').val(), $('#sensor #list option:selected').text(),$('#filter #from').val(),$('#filter #to').val());
        }
        return false;
    });

    $('#summary #update').click(function(){
        loadRelations();
        return false;
    });

    $('#summary #add').click(function(){
        createRelation();
        $('#summary #new').slideToggle('fast', function() {});
        return false;
    });

    $('#activities a').attr('href', "");

    /*Carica le informazioni di tutti i nodi presenti in Perla*/
    $('#activities #nodi').click(function (){
        allNodes();
        return false;
    });
    
    /*Carica le informazioni di tutti i sensori di perla*/
    $('#activities #sensori').click(function (){
        allSensors();
        return false;
    });

    $('#extra-infos-wrapper button').click(function(){
        $('#extra-infos-wrapper').fadeOut('def', function(){});
        $('#extra-infos-background').fadeOut('def', function(){});
    });

    $('#summary #show').click(function(){
        $('#summary #new').slideToggle('fast', function() {});
    });

    $('#summary #force-update').click(function(){
        updateAll();
    }).attr("disabled", "disabled");

    /* Carica i nodi al caricamento della pagina */
    loadNodes(['#node #list', '#new #nodes']);

    /* Carica le relazioni */
    loadRelations();

});